package org.odst.transformer;

import java.util.List;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.odst.controller.SheetTransformationController;
import org.odst.processor.RowProcessor;
import org.odst.tag.Block;
import org.odst.transformation.ResultTransformation;

/**
 * Controls a list of transformers
 * @author Leonid Vysochyn
 * @author Cyril PODER
 */
public class ChainTransformer{
    protected final Log log = LogFactory.getLog(getClass());

    List<RowTransformer> transformers;
    Sheet sheet;
    List<RowProcessor> rowProcessors;
    Row parentRow;

    public ChainTransformer(List<RowTransformer> transformers, Sheet sheet, List<RowProcessor> rowProcessors, Row parentRow) {
        this.transformers = transformers;
        this.sheet = sheet;
        this.rowProcessors = rowProcessors;
        this.parentRow = parentRow;
    }

    public ChainTransformer(List<RowTransformer> transformers, List<RowProcessor> rowProcessors, Row parentRow) {
        this.transformers = transformers;
        this.rowProcessors = rowProcessors;
        this.parentRow = parentRow;
    }

    public ChainTransformer(List<RowTransformer> transformers, List<RowProcessor> rowProcessors) {
        this.transformers = transformers;
        this.rowProcessors = rowProcessors;
    }

    public ChainTransformer(List<RowTransformer> transformers) {
        this.transformers = transformers;
    }



    ResultTransformation transform(SheetTransformationController stc, SheetTransformer sheetTransformer){
        ResultTransformation resultTransformation = new ResultTransformation();
        for (RowTransformer rowTransformer : transformers) {
            Block transformationBlock = rowTransformer.getTransformationBlock();
            transformationBlock = resultTransformation.transformBlock( transformationBlock );
            rowTransformer.setTransformationBlock( transformationBlock );
            log.debug(rowTransformer.getClass().getName() + ", " + rowTransformer.getTransformationBlock());
            //rowTransformer
            Row row = rowTransformer.getRow();
            row.setParentRow( parentRow );
            log.debug("Processing row : " + row.getRowIndex());
            applyRowProcessors(row );
//            try {
//				sheet.getHssfWorkbook().save("beforeTransformBlock.ods");
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
            resultTransformation.add( rowTransformer.transform(stc, sheetTransformer/*, resultTransformation */) );
//            try {
//				sheet.getHssfWorkbook().save("afterTransformBlock.ods");
//			} catch (Exception e) {
//				e.printStackTrace();
//			}

        }
        return resultTransformation;
    }

    /**
     * Applies all registered RowProcessors to a row
     * @param row - {@link org.odst.transformer.Row} object with row information
     */
    private void applyRowProcessors(Row row) {
        for (RowProcessor rowProcessor : rowProcessors) {
            rowProcessor.processRow(row, sheet.getNamedCells());
        }
    }


}
